package com.example.demo.utils;

import java.text.Collator;
import java.util.Locale;

public class ChineseToEnglish {
    public static String convertToEnglish(String chineseString) {
        StringBuilder englishString = new StringBuilder();
        Collator collator = Collator.getInstance(Locale.CHINA);
        for (char c : chineseString.toCharArray()) {
            if (Character.isLetter(c)) {
                String[] pinyinArray = collator.getCollationKey(String.valueOf(c)).getSourceString().split(" ");
                for (String pinyin : pinyinArray) {
                    if (!pinyin.isEmpty()) {
                        englishString.append(Character.toUpperCase(pinyin.charAt(0)));
                    }
                }
            } else {
                englishString.append(c);
            }
        }
        return englishString.toString();
    }
}